package com.hujia.hjsurprisemeal.repository;

import com.hujia.hjsurprisemeal.model.Dish;
import com.hujia.hjsurprisemeal.repository.DishMapper;
import com.hujia.hjsurprisemeal.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Override
    public boolean checkInventory(Integer dishId) {
        Dish dish = dishMapper.selectById(dishId);
        return dish != null && dish.getStock() > 0;
    }

    @Override
    public void decrementInventory(Integer dishId) {
        Dish dish = dishMapper.selectById(dishId);
        if (dish != null && dish.getStock() > 0) {
            dish.setStock(dish.getStock() - 1);
            dishMapper.updateById(dish);
        }
    }

    @Override
    public List<Dish> getDishesByIds(List<Integer> dishIds) {
        return dishMapper.selectBatchIds(dishIds);
    }

    @Override
    public Double getPrice(Integer dishId) {
        Dish dish = dishMapper.selectById(dishId);
        return dish != null ? dish.getPrice() : 0.0;
    }
}